$(function() {
	var opt = {
		debugLevel: "infor", //fatal,error,warn,infor
		//来电回调接口
		OnIncomingCall: function(callid, caller, len, data) {
			//获取callId
			Demo.setTipMsg("您有来电信息");
			Demo.isIncoming = true;
			if (Demo.callType !== Demo.CALL_TYPES.CALL_OUT) {
				Demo.callType = Demo.CALL_TYPES.INCOMING; //标识是来电
			}
			
			try{
				UCSConnect.AnswerCall();
				Demo.setTipMsg("电话已接听，正在通话中");
		    Demo.callStatus = "answer";
			}catch(e){
				alert(e);
			}
		},
		// 被叫接听电话回调
		OnCallAnswer: function() {
			Demo.setTipMsg("正在通话中");
		},
		//呼叫被释放回调接口
		OnCallGiveup: function(status) {
			if (status === 0) {
				Demo.setTipMsg("坐席挂断电话");
			} else if (status === 1) {
				Demo.setTipMsg("客户挂断电话");
			} else if (status === 2) {
				Demo.setTipMsg("坐席因欠费而终止电话");
			} else if (status === 10) {
				Demo.setTipMsg("因系统异常而挂断电话");
			} else if (status === 99) {
				Demo.setTipMsg("因其他原因而终止通话");
			}
		},
		//退出结果回调 0成功 1 失败
		OnLogoutRet: function(code) {
			if (code == Demo.LOGOUT_STATUS.SUCCESS) {
				Demo.setTipMsg("请先登录");
				ret = UCSConnect.init(
      		_appId,
      		opt,
      		function() {
      			Demo.init();
      		}
      	);
      	if (ret === false) {
      		alert("初始化UCSConnect失败");
      	} else {
          Demo.doLogin(_clientId, _clientPwd, _appId);
      	};
					
			} else if (code == Demo.LOGOUT_STATUS.FAILED) {
				Demo.setTipMsg("退出失败");
			}
		},
		// 登陆成功回调函数 响应码   描述 0: 成功（SUCCESS）	1: 超时（TIMEOUT）2：用户名非法（INVALID_USERNAME） 3：密码非法（INVALID_PASSWORD）99：其他（OTHER）
		OnLoginRet: function(responseCode) {
			if (responseCode === Demo.LOGIN_STATUS.SUCCESS) {
				Demo.setTipMsg("登录成功");
				isValid = true;
			} else if (responseCode === Demo.LOGIN_STATUS.TIMEOUT) {
				Demo.setTipMsg("登录超时");
				isValid = false;
			} else if (responseCode === Demo.LOGIN_STATUS.INVALID_USERNAME) {
				Demo.setTipMsg("用户名错误");
				isValid = false;
			} else if (responseCode === Demo.LOGIN_STATUS.INVALID_PASSWORD) {
				Demo.setTipMsg("密码错误");
				isValid = false;
			} else if (responseCode === Demo.LOGIN_STATUS.INVALID_APPID) {
				Demo.setTipMsg("请填写有效的APPId");
				isValid = false;
			}
		},
		OnCallRing: function() {
			Demo.setTipMsg("被叫正在振铃");
		},
		//外呼状态通知回调
		OnOutCallStatus: function(callId, ret) {
		  softphoneStateChange(ret);
			if (ret === Demo.CALL_OUT_STATUS.TIMEOUT) {
				Demo.setTipMsg("呼叫失败:超时");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_REFUSE) {
				Demo.setTipMsg("呼叫失败:坐席拒绝");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_NO_ANSWER) {
				Demo.setTipMsg("呼叫失败:坐席无应答");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_INEXISTENCE) {
				Demo.setTipMsg("呼叫失败:坐席不存在");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_CALL_FAILED) {
				Demo.setTipMsg("坐席呼叫失败");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_RINGING) {
				Demo.setTipMsg("坐席正在振铃");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_ANSWER) {
				Demo.setTipMsg("坐席已接听，正在呼叫对方");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_NO_ANSWER) {
				Demo.setTipMsg("客户无应答");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_INEXISTENCE) {
				Demo.setTipMsg("客户电话不存在");
  		} else if (ret === Demo.CALL_OUT_STATUS.CALLED_CALL_FAILED) {
				Demo.setTipMsg("客户呼叫失败");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_RINGING) {
				Demo.setTipMsg("客户正在振铃");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_ANSWER) {
				Demo.setTipMsg("客户已接听");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_REFUSE) {
				Demo.setTipMsg("客户拒接接听");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLER_HANG_UP) {
				Demo.setTipMsg("坐席挂机");
			} else if (ret === Demo.CALL_OUT_STATUS.CALLED_HANG_UP) {
				Demo.setTipMsg("客户挂机");
			} else if (ret === Demo.CALL_OUT_STATUS.M_PERMISSION_REQUESTED) {
				Demo.setTipMsg("请求获取您的音频");				
			} else if (ret === Demo.CALL_OUT_STATUS.M_PERMISSION_REQUESTED) {
				Demo.setTipMsg("获取音频失败");
			} else if (ret === Demo.CALL_OUT_STATUS.M_PERMISSION_OK) {
				if (Demo.callStatus == "answer" || Demo.callStatus === "refuse") {
				} else {
					if (Demo.callType === Demo.CALL_TYPES.INCOMING) {
						Demo.setTipMsg("您有来电，正在振铃...");
					} else if (Demo.callType === Demo.CALL_TYPES.CALL_OUT) {
						Demo.setTipMsg("正在呼叫...");
					}
				}
			} else if (ret === Demo.CALL_OUT_STATUS.UN_KNOWN) {
				Demo.setTipMsg("由于未知异常，呼叫失败");
			}
		},
		OnSubscribeRet: function(code){
			if(code === 0){//订阅成功
				Demo.setTipMsg("订阅成功");
			}else if(code === 1){//订阅失败
				Demo.setTipMsg("订阅失败");
			}else if(code === 2){//取消订阅成功
				Demo.setTipMsg("已成功取消订阅");
			}else if(code === 3){//取消订阅失败
				Demo.setTipMsg("取消订阅失败");
			}
		},
		OnExcepting: function(code) {
			if (code === 0) { //异地登录
			} else if (code === 1) { //网络断开
				Demo.setTipMsg("与服务器断开连接");
				setTimeout(function() {
					Demo.setTipMsg("请先登录");
					
					ret = UCSConnect.init(
        		_appId,
        		opt,
        		function() {
        			Demo.init();
        		}
        	);
        	if (ret === false) {
        		alert("初始化UCSConnect失败");
        	} else {
            Demo.doLogin(_clientId, _clientPwd, _appId);
        	};
				}, 5 * 1000);
			}
		}
	};
	
	var _appId = $("#voipAppID").val();
	var _clientId = $("#voipUserName").val();
	var _clientPwd = $("#voipUserPassword").val();
	var ret = UCSConnect.init(
		_appId,
		opt,
		function() {
			Demo.init();
		}
	);
	if (ret === false) {
		alert("初始化UCSConnect失败，可能您的浏览器不支持，推荐使用【搜狗浏览器】");
	} else {
    Demo.doLogin(_clientId, _clientPwd, _appId);
	};
});